package office.word.docx4j;

import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.fonts.fop.fonts.FontEventAdapter;
import org.docx4j.fonts.fop.fonts.autodetect.FontFileFinder;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.org.apache.fop.events.DefaultEventBroadcaster;

import java.net.URL;
import java.util.List;

/**
 * 描述
 *
 * @author: zhanglu
 * @date: 2023/7/13 15:38
 * @modify: 修改人信息, 修改内容 2023/7/13 15:38
 */
public class WordTest {

    public static void main(String[] args) throws Exception {
        String in = "/Users/zhanglu/Desktop/副本8d86ba899c67ab387cc80389c1fd18cc.docx";
        String out = "/Users/zhanglu/Desktop/tmp/docx4j/source2.docx";
        WordprocessingMLPackage wp01 = get(in);
//        WordprocessingMLPackage wp02 = get(out);
        List<Object> s1s = wp01.getMainDocumentPart().getContents().getContent();
//        List<Object> s2s = wp01.getMainDocumentPart().getContents().getContent();
        s1s.stream().forEach(System.out::println);
        System.out.println(1);
    }

    public static WordprocessingMLPackage get(String in) throws Exception {
        FontFileFinder fontFileFinder = new FontFileFinder(new FontEventAdapter(new DefaultEventBroadcaster()));
//        List<URL> fontFileList = fontFileFinder.find("/Users/zhanglu/Desktop/tmp/Fonts");
//        fontFileList.forEach(o -> {
//            try {
//                PhysicalFonts.addPhysicalFont(o.toURI());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File(in));
        Mapper fontMapper = new IdentityPlusMapper();
        wordMLPackage.setFontMapper(fontMapper);
        fontMapper.put("方正仿宋", PhysicalFonts.get("STFangSong"));
        fontMapper.put("方正仿宋_GBK", PhysicalFonts.get("STFangSong"));
        fontMapper.put("STFangSong", PhysicalFonts.get("STFangSong"));
        fontMapper.put("Calibri", PhysicalFonts.get("Calibri"));
        fontMapper.put("Symbol", PhysicalFonts.get("Symbol"));
        fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
        fontMapper.put("微软雅黑", PhysicalFonts.get("Microsoft Yahei"));
        fontMapper.put("黑体", PhysicalFonts.get("SimHei"));
        fontMapper.put("楷体", PhysicalFonts.get("KaiTi"));
        fontMapper.put("新宋体", PhysicalFonts.get("NSimSun"));
        fontMapper.put("华文行楷", PhysicalFonts.get("STXingkai"));
        fontMapper.put("华文仿宋", PhysicalFonts.get("STFangsong"));
        fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
        fontMapper.put("幼圆", PhysicalFonts.get("YouYuan"));
        fontMapper.put("华文宋体", PhysicalFonts.get("STSong"));
        fontMapper.put("华文中宋", PhysicalFonts.get("STZhongsong"));
        fontMapper.put("等线", PhysicalFonts.get("SimSun"));
        fontMapper.put("等线 Light", PhysicalFonts.get("SimSun"));
        fontMapper.put("华文琥珀", PhysicalFonts.get("STHupo"));
        fontMapper.put("华文隶书", PhysicalFonts.get("STLiti"));
        fontMapper.put("华文新魏", PhysicalFonts.get("STXinwei"));
        fontMapper.put("华文彩云", PhysicalFonts.get("STCaiyun"));
        fontMapper.put("方正姚体", PhysicalFonts.get("FZYaoti"));
        fontMapper.put("方正舒体", PhysicalFonts.get("FZShuTi"));
        fontMapper.put("华文细黑", PhysicalFonts.get("STXihei"));
        fontMapper.put("宋体扩展",PhysicalFonts.get("simsun-extB"));
        fontMapper.put("仿宋_GB2312",PhysicalFonts.get("FangSong_GB2312"));
        fontMapper.put("新細明體",PhysicalFonts.get("SimSun"));
        return wordMLPackage;
    }

}
